import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;

import Pieces.BlockType;
import Pieces.Board;


public class GUI extends JFrame{

	JPanel nextPieceBox;
	JTextField scoreBox;
	BoardPanel board;
	JPanel rightSide;
	JButton playPause;
	JPanel nextPiece;
	JButton newGame;
	KeyListener left;
	int buttonPressed = 0;
	private boolean paused = false;
    Board gameBoard;

	
	public boolean getPause(){
		return paused;
	}
	
	public void resetFocus(){
		this.requestFocus();
	}
	
	ActionListener playListener = new ActionListener(){
		@Override
		public void actionPerformed(ActionEvent arg0) {
				paused = !paused;
				resetFocus();
		}		
	};
	

	ActionListener newGameListener = new ActionListener(){
		@Override
		public void actionPerformed(ActionEvent arg0) {
			// make new game
			resetFocus();
		}		
	};

	KeyListener listener = new KeyListener() {
		public void keyPressed(KeyEvent e) {
			dumpInfo("Pressed", e);
		}

		public void keyReleased(KeyEvent e) {
			dumpInfo("Released", e);
		}

		public void keyTyped(KeyEvent e) {
			dumpInfo("Typed", e);
		}

		private void dumpInfo(String s, KeyEvent e) {
			buttonPressed = e.getKeyCode();
			System.out.println(buttonPressed);
			scoreBox.setText(""+buttonPressed);
		}
	};

	public int getButton(){
		int temp = buttonPressed;
		buttonPressed = 0;
		return temp;
	}
	
	public void showBoard(){
		BlockType[][] brd = gameBoard.getBoard();
		for(int i = 0; i < Board.BOARD_HEIGHT; i++){
			for(int j = 0; j < Board.BOARD_WIDTH; j++){
				board.changeColor(i, j, brd[i][j].getBlockColor());
			}
		}
		repaint();
	}


	public GUI(Board theBoard){

		super("Tetris 9000");
		gameBoard = theBoard;
		// Control Panel
		playPause = new JButton("Play / Pause");
		newGame = new JButton("New Game");
		scoreBox = new JTextField(10);
		scoreBox.setEditable(false);
		
		playPause.addActionListener(playListener);
		newGame.addActionListener(newGameListener);
		
		JPanel controlPanel = new JPanel();
		controlPanel.setLayout(new GridLayout(20,1));
		controlPanel.add(playPause);
		controlPanel.add(newGame);
		controlPanel.add(scoreBox);

		// Main Container
		Container content = this.getContentPane();
		content.setBackground(Color.white);
		//	    content.setLayout(new BorderLayout());
		//	    content.add(controlPanel,BorderLayout.EAST);

		board = new BoardPanel();
		board.setPreferredSize(new Dimension(400, 837));
		rightSide = new JPanel();
		nextPiece = new JPanel();
		nextPiece.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
		content.setLayout(new BoxLayout(content, BoxLayout.X_AXIS));
		content.add(board);
		board.setSize(board.getPreferredSize());
		board.setMinimumSize(board.getPreferredSize());
		board.setMaximumSize(board.getPreferredSize());
	

		rightSide.setLayout(new BoxLayout(rightSide, BoxLayout.Y_AXIS));
		
		nextPiece.setSize(250, 250);
		nextPiece.setPreferredSize(nextPiece.getSize());
		nextPiece.setMinimumSize(nextPiece.getPreferredSize());
		nextPiece.setMaximumSize(nextPiece.getPreferredSize());
		nextPiece.setBackground(Color.black);

		newGame.setPreferredSize(playPause.getPreferredSize());
		newGame.setSize(playPause.getPreferredSize());
		newGame.setMinimumSize(playPause.getPreferredSize());
		newGame.setMaximumSize(playPause.getPreferredSize());

		scoreBox.setPreferredSize(playPause.getPreferredSize());
		scoreBox.setSize(playPause.getPreferredSize());
		scoreBox.setMinimumSize(playPause.getPreferredSize());
		scoreBox.setMaximumSize(playPause.getPreferredSize());
		scoreBox.setBackground(Color.white);

		rightSide.add(nextPiece);
		rightSide.add(scoreBox);
		rightSide.add(Box.createVerticalGlue());
		rightSide.add(playPause);
		rightSide.add(Box.createRigidArea(new Dimension(0, 20)));
		rightSide.add(newGame);

		content.add(rightSide);

		// Frame work
		this.setSize(650, 837);
		this.addKeyListener(listener);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.requestFocus();// do this!!!!!!!!!!!
	}


	
}
